/*
 * digital_pins.h
 *
 * Created: 21.7.2013 19:42:43
 *  Author: Ferda
 */ 

#include <avr/io.h>

#ifndef DIGITAL_PINS_H_
#define DIGITAL_PINS_H_

#ifndef INPUT
	#define INPUT 0
#endif

#ifndef OUTPUT
	#define OUTPUT 1
#endif

#ifndef HIGH
	#define HIGH 1
#endif

#ifndef LOW
	#define LOW 0
#endif

//==============================================================================
#ifdef __cplusplus
	extern "C" {
#endif

//==============================================================================
#ifdef __cplusplus
	}
#endif
//==============================================================================

#define MASK(bit) (1 << (bit))
#define _SetPinMode(pin, val) do { if (val) DIO_ ## pin ## _DDR |= MASK(DIO_ ## pin ## _PIN); else DIO_ ## pin ## _DDR &= ~MASK(DIO_ ## pin ## _PIN); }while(0)
#define _WritePin(pin, val) do { if (val) { DIO_ ## pin ## _WPORT |= MASK(DIO_ ## pin ## _PIN); } else { DIO_ ## pin ## _WPORT &= ~MASK(DIO_ ## pin ## _PIN);} }while(0)
#define _ReadPin(pin) ((bool)(DIO_ ## pin ## _RPORT & MASK(DIO_ ## pin ## _PIN)))

#define DirectionPort(pin) DIO_##pin##_DDRA
#define WritePort(pin) DIO_##pin##_WPORT
#define ReadPort(pin) DIO_##pin##_RPORT
#define PinBit(pin) DIO_##pin##_PIN
//===============================================================================

#define SetPinMode(pin, mode) _SetPinMode(pin, mode)
#define WritePin(pin, val) _WritePin(pin, val)
#define ReadPin(pin) _ReadPin(pin)

//==================================================================================

//=== PORT A ==============
// pin 0
#define DIO_0_PIN PINA0			 
#define DIO_0_RPORT PINA
#define DIO_0_WPORT PORTA
#define DIO_0_DDR DDRA

// pin 1
#define DIO_1_PIN	PINA1
#define DIO_1_RPORT PINA
#define DIO_1_WPORT PORTA
#define DIO_1_DDR DDRA

// pin 2
#define DIO_2_PIN	PINA2
#define DIO_2_RPORT PINA
#define DIO_2_WPORT PORTA
#define DIO_2_DDR DDRA

// pin 3
#define DIO_3_PIN	PINA3
#define DIO_3_RPORT PINA
#define DIO_3_WPORT PORTA
#define DIO_3_DDR DDRA

// pin 4
#define DIO_4_PIN	PINA4
#define DIO_4_RPORT PINA
#define DIO_4_WPORT PORTA
#define DIO_4_DDR DDRA

// pin 5
#define DIO_5_PIN	PINA5
#define DIO_5_RPORT PINA
#define DIO_5_WPORT PORTA
#define DIO_5_DDR DDRA

// pin 6
#define DIO_6_PIN	PINA6
#define DIO_6_RPORT PINA
#define DIO_6_WPORT PORTA
#define DIO_6_DDR DDRA

// pin 7
#define DIO_7_PIN	PINA7
#define DIO_7_RPORT PINA
#define DIO_7_WPORT PORTA
#define DIO_7_DDR DDRA

//=== PORT B =================
// pin 8
#define DIO_8_PIN PINB0
#define DIO_8_RPORT PINB
#define DIO_8_WPORT PORTB
#define DIO_8_DDR DDRB

// pin 9
#define DIO_9_PIN	PINB1
#define DIO_9_RPORT PINB
#define DIO_9_WPORT PORTB
#define DIO_9_DDR DDRB

// pin 10
#define DIO_10_PIN	PINB2
#define DIO_10_RPORT PINB
#define DIO_10_WPORT PORTB
#define DIO_10_DDR DDRB

// pin 11
#define DIO_11_PIN	PINB3
#define DIO_11_RPORT PINB
#define DIO_11_WPORT PORTB
#define DIO_11_DDR DDRB

// pin 12
#define DIO_12_PIN	PINB4
#define DIO_12_RPORT PINB
#define DIO_12_WPORT PORTB
#define DIO_12_DDR DDRB

// pin 13
#define DIO_13_PIN	PINB5
#define DIO_13_RPORT PINB
#define DIO_13_WPORT PORTB
#define DIO_13_DDR DDRB

// PIN 14
#define DIO_14_PIN	PINB6
#define DIO_14_RPORT PINB
#define DIO_14_WPORT PORTB
#define DIO_14_DDR DDRB

// PIN 15
#define DIO_15_PIN	PINB7
#define DIO_15_RPORT PINB
#define DIO_15_WPORT PORTB
#define DIO_15_DDR DDRB

//=== PORT C =================

// pin 16
#define DIO_16_PIN PINC0
#define DIO_16_RPORT PINC
#define DIO_16_WPORT PORTC
#define DIO_16_DDR DDRC

// pin 17
#define DIO_17_PIN	PINC1
#define DIO_17_RPORT PINC
#define DIO_17_WPORT PORTC
#define DIO_17_DDR DDRC

// pin 18
#define DIO_18_PIN	PINC2
#define DIO_18_RPORT PINC
#define DIO_18_WPORT PORTC
#define DIO_18_DDR DDRC

// pin 19
#define DIO_19_PIN	PINC3
#define DIO_19_RPORT PINC
#define DIO_19_WPORT PORTC
#define DIO_19_DDR DDRC

// pin 20
#define DIO_20_PIN	PINC4
#define DIO_20_RPORT PINC
#define DIO_20_WPORT PORTC
#define DIO_20_DDR DDRC

// pin 21
#define DIO_21_PIN	PINC5
#define DIO_21_RPORT PINC
#define DIO_21_WPORT PORTC
#define DIO_21_DDR DDRC

// PIN 22
#define DIO_22_PIN	PINC6
#define DIO_22_RPORT PINC
#define DIO_22_WPORT PORTC
#define DIO_22_DDR DDRC

// PIN 23
#define DIO_23_PIN	PINC7
#define DIO_23_RPORT PINC
#define DIO_23_WPORT PORTC
#define DIO_23_DDR DDRC

//=== PORTD D ================

// pin 24
#define DIO_24_PIN PIND0
#define DIO_24_RPORT PIND
#define DIO_24_WPORT PORTD
#define DIO_24_DDR DDRD

// pin 25
#define DIO_25_PIN	PIND1
#define DIO_25_RPORT PIND
#define DIO_25_WPORT PORTD
#define DIO_25_DDR DDRD

// pin 26
#define DIO_26_PIN	PIND2
#define DIO_26_RPORT PIND
#define DIO_26_WPORT PORTD
#define DIO_26_DDR DDRD

// pin 27
#define DIO_27_PIN	PIND3
#define DIO_27_RPORT PIND
#define DIO_27_WPORT PORTD
#define DIO_27_DDR DDRD

// pin 28
#define DIO_28_PIN	PIND4
#define DIO_28_RPORT PIND
#define DIO_28_WPORT PORTD
#define DIO_28_DDR DDRD

// pin 29
#define DIO_29_PIN	PIND5
#define DIO_29_RPORT PIND
#define DIO_29_WPORT PORTD
#define DIO_29_DDR DDRD

// PIN 30
#define DIO_30_PIN	PIND6
#define DIO_30_RPORT PIND
#define DIO_30_WPORT PORTD
#define DIO_30_DDR DDRD

// PIN 31
#define DIO_31_PIN	PIND7
#define DIO_31_RPORT PIND
#define DIO_31_WPORT PORTD
#define DIO_31_DDR DDRD

//=== PORT E =================

// pin 32
#define DIO_32_PIN PINE0
#define DIO_32_RPORT PINE
#define DIO_32_WPORT PORTE
#define DIO_32_DDR DDRE

// pin 33
#define DIO_33_PIN	PINE1
#define DIO_33_RPORT PINE
#define DIO_33_WPORT PORTE
#define DIO_33_DDR DDRE

// pin 34
#define DIO_34_PIN	PINE2
#define DIO_34_RPORT PINE
#define DIO_34_WPORT PORTE
#define DIO_34_DDR DDRE

// pin 35
#define DIO_35_PIN	PINE3
#define DIO_35_RPORT PINE
#define DIO_35_WPORT PORTE
#define DIO_35_DDR DDRE

// pin 36
#define DIO_36_PIN	PINE4
#define DIO_36_RPORT PINE
#define DIO_36_WPORT PORTE
#define DIO_36_DDR DDRE

// pin 37
#define DIO_37_PIN	PINE5
#define DIO_37_RPORT PINE
#define DIO_37_WPORT PORTE
#define DIO_37_DDR DDRE

// PIN 38
#define DIO_38_PIN	PINE6
#define DIO_38_RPORT PINE
#define DIO_38_WPORT PORTE
#define DIO_38_DDR DDRE

// PIN 39
#define DIO_39_PIN	PINE7
#define DIO_39_RPORT PINE
#define DIO_39_WPORT PORTE
#define DIO_39_DDR DDRE

//=== PORT F =================

// pin 40
#define DIO_40_PIN PINF0
#define DIO_40_RPORT PINF
#define DIO_40_WPORT PORTF
#define DIO_40_DDR DDRF

// pin 41
#define DIO_41_PIN	PINF1
#define DIO_41_RPORT PINF
#define DIO_41_WPORT PORTF
#define DIO_41_DDR DDRF

// pin 42
#define DIO_42_PIN	PINF2
#define DIO_42_RPORT PINF
#define DIO_42_WPORT PORTF
#define DIO_42_DDR DDRF

// pin 43
#define DIO_43_PIN	PINF3
#define DIO_43_RPORT PINF
#define DIO_43_WPORT PORTF
#define DIO_43_DDR DDRF

// pin 44
#define DIO_44_PIN	PINF4
#define DIO_44_RPORT PINF
#define DIO_44_WPORT PORTF
#define DIO_44_DDR DDRF

// pin 45
#define DIO_45_PIN	PINF5
#define DIO_45_RPORT PINF
#define DIO_45_WPORT PORTF
#define DIO_45_DDR DDRF

// PIN 46
#define DIO_46_PIN	PINF6
#define DIO_46_RPORT PINF
#define DIO_46_WPORT PORTF
#define DIO_46_DDR DDRF

// PIN 47
#define DIO_47_PIN	PINF7
#define DIO_47_RPORT PINF
#define DIO_47_WPORT PORTF
#define DIO_47_DDR DDRF

//=== PORT G =================

// pin 48
#define DIO_48_PIN PING0
#define DIO_48_RPORT PING
#define DIO_48_WPORT PORTG
#define DIO_48_DDR DDRG

// pin 49
#define DIO_49_PIN	PING1
#define DIO_49_RPORT PING
#define DIO_49_WPORT PORTG
#define DIO_49_DDR DDRG

// pin 50
#define DIO_50_PIN	PING2
#define DIO_50_RPORT PING
#define DIO_50_WPORT PORTG
#define DIO_50_DDR DDRG

// pin 51
#define DIO_51_PIN	PING3
#define DIO_51_RPORT PING
#define DIO_51_WPORT PORTG
#define DIO_51_DDR DDRG

// pin 52
#define DIO_52_PIN	PING4
#define DIO_52_RPORT PING
#define DIO_52_WPORT PORTG
#define DIO_52_DDR DDRG

//=== END PINS CONFIGURATIONS ==================

#endif /* DIGITAL_PINS_H_ */